package com.yunjian.core.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunjian.core.entity.SysCaptchaEntity;
import com.yunjian.core.service.impl.SysCaptchaServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class CleanExpiredDataJob {

    @Autowired
    private SysCaptchaServiceImpl sysCaptchaServiceImpl;

    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void cleanExpiredCaptcha() {
        log.info("执行清理过期的二维码数据");
        int count = sysCaptchaServiceImpl.count(new QueryWrapper<SysCaptchaEntity>().lt("expire_time", new Date()));
        log.info("检测到过期二维码[{}]个", count);
        if (count > 0) {
            sysCaptchaServiceImpl.remove(new QueryWrapper<SysCaptchaEntity>().lt("expire_time", new Date()));
        }
        log.info("过期二维码清理完成");
    }

}
